import { _decorator, Component, Collider, ITriggerEvent } from 'cc';
import { Contant } from '../framework/Contant';
import { gameManger } from '../framework/gameManger';
const { ccclass } = _decorator;

const OUTOFBOUNCE = 50

@ccclass('EnemyPlan')
export class EnemyPlane extends Component {
    
    private _enemySpeed = 0;

    public _gameManger: gameManger = null

    onEnable() {
        const collider = this.getComponent(Collider)
        collider.on('onTriggerEnter', this._onTriggerEnter, this);
    }

    onDisable () {
        const collider = this.getComponent(Collider);
        collider.off('onTriggerEnter', this._onTriggerEnter, this);
    }

    update(deltaTime: number) {
        const pos = this.node.position
        const movpos = pos.z + this._enemySpeed
        this.node.setPosition(pos.x, pos.y, movpos)
        // 超出屏幕自动销毁
        if(movpos > OUTOFBOUNCE){
            this.node.destroy()
        }
    }

    public show(speed:number, gameManger: gameManger) {
        this._enemySpeed = speed
        this._gameManger = gameManger
    }

    _onTriggerEnter(event : ITriggerEvent) {
        const eventGrop = event.otherCollider.getGroup()

        if(eventGrop === Contant.CollisionType.SELF_BULLET || eventGrop === Contant.CollisionType.SELF_PLANE){
            this._gameManger.addScope()
            this.node.destroy()
        }
    }
}


